#####################################
##REPLICATION FOR MCGHEE, ET AL.,####
##"A PRIMARY CAUSE OF PARTISANSHIP"##
#####################################

#Table A10 (Appendix)#

library(Zelig)
library(sandwich)
setwd("XXXX") #ENTER DIRECTORY PATHNAME IN PLACE OF XXXX

#Democrats#
legyrd <- read.csv("legislator primaries.dem.csv", header=TRUE)

legyrd$yrcnt <- (legyrd$elec-1992)/2

#define years & states as factors for FE regression#
legyrd$icpsrst <- as.factor(legyrd$icpsrst)
legyrd$yrcnt <- as.factor(legyrd$yrcnt)

legyrd$ideowt <- NULL

#Model results:  all data#
z.out <- zelig(pred_np ~ semicl + semiop + open + nonpart + 
	icpsrst + yrcnt, robust=list(method="vcovHAC"), 
	model="ls", data=legyrd)
summary(z.out)
length(legyrd$icpsrst)

#Model results:  pres-only data (original results)#
legyrd <- na.omit(legyrd)
z.out <- zelig(pred_np ~ semicl + semiop + open + nonpart + 
	icpsrst + yrcnt, robust=list(method="vcovHAC"), 
	model="ls", data=legyrd)
summary(z.out)
length(legyrd$icpsrst)

#Republicans#
legyrr <- read.csv("legislator primaries.rep.csv", header=TRUE)

legyrr$yrcnt <- (legyrr$elec-1992)/2

legyrr$ideowt <- NULL

#define years & states as factors for FE regression#
legyrr$icpsrst <- as.factor(legyrr$icpsrst)
legyrr$yrcnt <- as.factor(legyrr$yrcnt)

#Model results:  all data#
z.out <- zelig(pred_np ~ semicl + semiop + open + nonpart + 
	icpsrst + yrcnt, robust=list(method="vcovHAC"), 
	model="ls", data=legyrr)
summary(z.out)
length(legyrr$icpsrst)

#Model results:  pres-only data (original results)#
legyrr <- na.omit(legyrr)
z.out <- zelig(pred_np ~ semicl + semiop + open + nonpart + 
	icpsrst + yrcnt, robust=list(method="vcovHAC"), 
	model="ls", data=legyrr)
summary(z.out)
length(legyrr$icpsrst)


